<?php /* Smarty version 2.6.20, created on 2011-11-18 16:48:40
         compiled from _core/_include/module/_mvc/control/handlesearchauditexpense.tpl */ ?>
<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
smarty_core_load_plugins(array('plugins' => array(array('modifier', 'escape', '_core/_include/module/_mvc/control/handlesearchauditexpense.tpl', 132, false),)), $this); ?>
function HandleSearchAuditExpense(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  this._popupBox = new PopupBox();
  
  this._callbackFunc = (options && options.callbackFunc) ? options.callbackFunc : null;
  this._search = (options && options.search) ? options.search : null;
  this._expenses = (options && options.items) ? options.items : null;
  this._cond = (options && options.cond) ? options.cond : '1=1';
  
  if (!this._search) {
    this._search = DlmanageUtils.acctExpenseSearch();
  }
  this._createElements();
};

HandleSearchAuditExpense.prototype._createElements = function() {
  var _self = this;
  
  this._gui.cdateFrom.value = (this._search.cdateFrom) ? SimpleDate.format(this._search.cdateFrom) : '';
  this._gui.cdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.cdateFrom, function(sd) { _self._search.cdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  this._gui.cdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc(_self._search);
                                                  }
                                                };
  this._gui.cdateTo.value = (this._search.cdateTo) ? SimpleDate.format(this._search.cdateTo) : '';
  this._gui.cdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.cdateTo, function(sd) { _self._search.cdateTo = sd; context.focus(); }, false);
                                            return false;
                                         };
  this._gui.cdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc(_self._search);
                                               }
                                             };
                                             
  this._gui.pdateFrom.value = (this._search.pdateFrom) ? SimpleDate.format(this._search.pdateFrom) : '';
  this._gui.pdateFrom.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.pdateFrom, function(sd) { _self._search.pdateFrom = sd; context.focus(); }, false);
                                              return false;
                                            };
  this._gui.pdateFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc(_self._search);
                                                  }
                                                };
  this._gui.pdateTo.value = (this._search.pdateTo) ? SimpleDate.format(this._search.pdateTo) : '';
  this._gui.pdateTo.onclick = function() { var context = this;
                                            _self._changeDate.call(_self, this, _self._search.pdateTo, function(sd) { _self._search.pdateTo = sd; context.focus(); }, false);
                                            return false;
                                         };
  this._gui.pdateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc(_self._search);
                                               }
                                             };
  this._payees = []; this._particulars = [];
  for (var i = 0, il = this._expenses.length; i < il; i++) {
    var expense = this._expenses[i];
    exist1 = false; exist2 = false;
    for (var j = 0, jl = this._payees.length; j < jl; j++) {
      var p = this._payees[j];
      if (expense.payee == p.name) {
        exist1 = true;
      }
    }
    for (var k = 0, kl = this._particulars.length; k < kl; k++) {
      var p = this._particulars[k];
      if (expense.particular == p.name) {
        exist2 = true;
      }
    }
    if (!exist1) {
      this._payees[this._payees.length] = { name: expense.payee };
    }
    if (!exist2) {
      this._particulars[this._particulars.length] = {name: expense.particular};
    }
  }
  
  this._gui.payee.options[this._gui.payee.options.length] = new Option('');
  for (var i = 0, il = this._payees.length; i < il; i++) {
    var payee = this._payees[i];
    var option = new Option(payee.name);
    this._gui.payee.options[this._gui.payee.options.length] = option;
    if (option.text == this._search.payee){
      this._gui.payee.selectedIndex = this._gui.payee.options.length - 1;
    }
  }
  if (this._payees.length > 0) {
    this._search.payee = this._gui.payee.options[this._gui.payee.selectedIndex].text;
    this._gui.payee.onchange = function() { _self._search.payee = this.options[this.selectedIndex].text; };
  }

  this._gui.particular.options[this._gui.particular.options.length] = new Option('');
  for (var i = 0, il = this._particulars.length; i < il; i++) {
    var particular = this._particulars[i];
    var option = new Option(particular.name);
    this._gui.particular.options[this._gui.particular.options.length] = option;
    if (option.text == this._search.particular){
      this._gui.particular.selectedIndex = this._gui.particular.options.length - 1;
    }
  }
  if (this._particulars.length > 0) {
    this._search.particular = this._gui.particular.options[this._gui.particular.selectedIndex].text;
    this._gui.particular.onchange = function() { _self._search.particular = this.options[this.selectedIndex].text; };
  }
  
  this._gui.amount.value = this._search.amount;
  this._gui.amount.onchange = function() { _self._search.amount = this.value; };
  this._gui.amount.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                               if (code == 13) {
                                                 _self._callbackFunc(_self._search);
                                               }
                                             };
  this._gui.method.options[this._gui.method.options.length] = new Option('');
  for (var i = 0, il = ExpenseMethod.array().length; i < il; i++) {
    var method = ExpenseMethod.array()[i];
    var option = new Option(method);
    this._gui.method.options[this._gui.method.options.length] = option;
    if (option.text == this._search.method){
      this._gui.method.selectedIndex = this._gui.method.options.length - 1;
    }
  }
  if (ExpenseMethod.array().length > 0) {
    this._search.method = (this._gui.method.options[this._gui.method.selectedIndex].text == '<?php echo ((is_array($_tmp=@EXPENSE_METHOD_CASH)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') ? 'C' : (this._gui.method.options[this._gui.method.selectedIndex].text == '<?php echo ((is_array($_tmp=@EXPENSE_METHOD_BANK)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') ? 'B' : (this._gui.method.options[this._gui.method.selectedIndex].text == '<?php echo ((is_array($_tmp=@EXPENSE_METHOD_CHEQUE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') ? 'Q' : '';
    this._gui.method.onchange = function() { _self._search.method = (this.options[this.selectedIndex].text == '<?php echo ((is_array($_tmp=@EXPENSE_METHOD_CASH)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') ? 'C' : (this.options[this.selectedIndex].text == '<?php echo ((is_array($_tmp=@EXPENSE_METHOD_BANK)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') ? 'B' : (this.options[this.selectedIndex].text == '<?php echo ((is_array($_tmp=@EXPENSE_METHOD_CHEQUE)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') ? 'Q' : ''; };
  }
  
  this._gui.chequeNo.value = this._search.chequeNo;
  this._gui.chequeNo.onchange = function() { _self._search.chequeNo = this.value; };
  this._gui.chequeNo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                if (code == 13) {
                                                  _self._callbackFunc(_self._search);
                                                } else {
                                                  var div = document.createElement('div');
                                                  var pos1 = DOMUtils.findPos(_self._gui.chequeNo);
                                                  var pos = [pos1[0] + 1, pos1[1] + 82];
                                                  var query = "SELECT d.chequeNumber FROM ns_Expense as d WHERE " + _self._cond + " AND " + '((d.chequeNumber LIKE \'%' + _self._gui.chequeNo.value + '%\'))' + " ";
                                                  MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                                  new DialogSearchResult(div, _self._gui.chequeNo, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._search.chequeNo = _self._gui.chequeNo.value }, width: 195});
                                                }
                                            };
 this._gui.note.onchange = function() { _self._search.note = this.value; };
 this._gui.note.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                             if (code == 13) {
                                               _self._callbackFunc(_self._search);
                                             } else {
                                               var div = document.createElement('div');
                                               var pos1 = DOMUtils.findPos(_self._gui.note);
                                               var pos = [pos1[0] + 1, pos1[1] + 82];
                                               var query = "SELECT d.note FROM ns_Expense as d WHERE " + _self._cond + " AND " + '((d.note LIKE \'%' + _self._gui.note.value + '%\'))' + " ";
                                               MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                               new DialogSearchResult(div, _self._gui.note, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._search.note = _self._gui.note.value }, width: 195});
                                             }
                                           };
  this._gui.ng.defaultChecked = this._search.ng;
  this._gui.ng.onclick = function() { if (this.checked == true) {
                                         _self._search.ng = 1;
                                       } else {
                                         _self._search.ng = 0;
                                       }
                                     };
  this._gui.ne.defaultChecked = this._search.ne;
  this._gui.ne.onclick = function() { if (this.checked == true) {
                                         _self._search.ne = 1;
                                       } else {
                                         _self._search.ne = 0;
                                       }
                                     };
  this._gui.paid.defaultChecked = this._search.paid;
  this._gui.paid.onclick = function() { if (this.checked == true) {
                                           _self._search.paid = 1;
                                         } else {
                                           _self._search.paid = 0;
                                         }
                                       };
  this._gui.unpaid.defaultChecked = this._search.unpaid;
  this._gui.unpaid.onclick = function() { if (this.checked == true) {
                                             _self._search.unpaid = 1;
                                           } else {
                                             _self._search.unpaid = 0;
                                           }
                                         };
  this._gui.search.onclick = function() { _self._callbackFunc(_self._search);
                                         };
};

HandleSearchAuditExpense.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandleSearchAuditExpense.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandleSearchAuditExpense.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};

HandleSearchAuditExpense.toSql = function(search, lag) {
  var cfrom = parseInt(SimpleDate.toStamp(search.cdateFrom) + lag);
  var cto = parseInt(SimpleDate.toStamp(search.cdateTo) + lag);
  var pfrom = parseInt(SimpleDate.toStamp(search.pdateFrom) + lag);
  var pto = parseInt(SimpleDate.toStamp(search.pdateTo) + lag);
  var str = this.toString(search);
  return ((str != '1=1') ? str : '1=1') + ' AND ' + ((search.cdateFrom) ? '(d.createdDate >= \'' + cfrom + '\')' : '1 = 1') + ' AND ' + ((search.cdateTo) ? '(d.createdDate <= \'' + cto + '\')' : '1 = 1') + ' AND ' + ((search.pdateFrom) ? '(d.payDate >= \'' + pfrom + '\')' : '1 = 1') + ' AND ' + ((search.pdateTo) ? '(d.payDate <= \'' + pto + '\')' : '1 = 1');
};

HandleSearchAuditExpense.toString = function(search) {
  var str = '1=1';
  
  str += (search.payee == '') ? '' : ' AND (d.payee LIKE \'%' + search.payee + '%\')';
  str += (search.particular == '') ? '' : ' AND (d.particular LIKE \'%' + encodeURI(search.particular) + '%\')';
  str += (search.amount == '') ? '' : ((isNaN(search.amount)) ? ' AND 1 = 0' : ' AND (d.amount=\'' + search.amount + '\')');
  str += (search.method == '') ? '' : ' AND (d.method=\'' + search.method + '\')';
  str += (search.chequeNo == '') ? '' : ' AND (d.chequeNumber =\'' + search.chequeNo + '\')';
  str += (search.note == '') ? '' : ' AND (d.note LIKE \'%' + search.note + '%\')';
  
  if (search.ng == 1  && search.ne ==1  && search.paid == 1 && search.unpaid == 1) {
    str += ' AND ((d.ng = 1) OR (d.ne = 1) OR (d.status = 2) OR (d.status = 1))';
  } else if (search.ng == 1 && search.ne == 1 && search.paid == 1) {
    str += ' AND ((d.ng = 1) OR (d.ne = 1) OR (d.status = 2))';
  } else if (search.ng == 1 && search.ne == 1 && search.unpaid == 1) {
    str += ' AND ((d.ng = 1) OR (d.ne = 1) OR (d.status = 1))';
  } else if (search.ng == 1 && search.paid == 1 && search.unpaid == 1) {
    str += ' AND ((d.ng = 1) OR (d.status = 2) OR (d.status = 1))';
  } else if (search.ne == 1 && search.paid == 1 && search.unpaid == 1) {
    str += ' AND ((d.ne = 1) OR (d.status = 2) OR (d.status = 1))';
  } else if (search.ng == 1 && search.ne == 1) {
    str += ' AND ((d.ng = 1) OR (d.ne = 1))';
  } else if (search.ng == 1 && search.paid == 1) {
    str += ' AND ((d.ng = 1) OR (d.status = 2))';
  } else if (search.ng == 1 && search.unpaid == 1) {
    str += ' AND ((d.ng = 1) OR (d.status = 1))';
  } else if (search.ne == 1 && search.paid == 1) {
    str += ' AND ((d.ne = 1) OR (d.status = 2))';
  } else if (search.ne == 1 && search.unpaid == 1) {
    str += ' AND ((d.ne = 1) OR (d.status = 1))';
  } else if (search.paid == 1 && search.unpaid == 1) {
    str += ' AND ((d.) OR (d.status = 1))';
  } else if (search.ng == 1) {
    str += ' AND (d.ng = 1)';
  }  else if (search.ne == 1) {
    str += ' AND (d.ne = 1)';
  } else if (search.paid == 1) {
    str += ' AND (d.status = 2)';
  } else if (search.unpaid == 1) {
    str += ' AND (d.status = 1)';
  }
  
  return str;
};